<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Article;
use App\Models\Circle;
use App\Models\Comment;
use App\Models\Notification;
use App\Models\Report;
use App\Models\User;
use App\Mongo\ArticleMon;
use App\Services\NotifyService;
use Illuminate\Http\Request;
use YangJiSen\CacheUserProvider\CacheForget;

class ReportController extends BaseController
{

    public function index(Request $request) {
        $input = $request->input();
        $search = ['title' => '', 'start_at' => '', 'end_at' => '', 'status' => 0, 'type' => 0];
        $enum_id = $request->input('enum_id');

        $list = Report::apply($search, $input)->withCertain('publisher', ['nickname'])
            ->withCertain('reporter', ['nickname'])->withCertain('article', ['title', 'file_path', 'type'])
            ->withCertain('enum', ['value'])->orderBy("id", "desc")
            ->when($enum_id, function ($q) use($enum_id) {
                $q->where('enum_id', $enum_id);
            })
            ->paginate(per_page());
        return $this->paginate($list);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
            'status' => 'required|in:2,3'
        ]);
        $ids= $request->input('ids');
        $arr_id = explode(",", $ids);
        foreach ($arr_id as $id) {
            $report = Report::find($id);
            if (!$report)
                return $this->failed('不存在');

            if ($report->status != 1)
                return $this->failed('已处理');

            $status = $request->input('status');
            if ($status == 2) {
                if ($report->type != 3) {
                    $article = Article::find($report->entity_id);
                    $arr_old_article_id = $article->circles()->get(['circles.id'])->pluck('id');
                    Circle::whereIn('id', $arr_old_article_id)->increment('feed_count', -1);
                    ArticleMon::where('id', $article->id)->delete();
                    $article->delete();
                    $keyword = $article->title;
                } else {
                    $comment = Comment::find($report->comment_id);
                    $keyword = $comment->message;
                    $parent_id = $comment->parent_id;
                    $entity_id = $comment->entity_id;
                    if ($parent_id == 0) {
                        Comment::where('parent_id', $comment->id)->delete();
                    }
                    $comment->delete();
                    $comment_num = Comment::where('entity_id', $entity_id)->count();
                    Article::where('id', $entity_id)->update(['comment_num' => $comment_num, 'latest_comment_at' => time()]);
                }
                $keyword = mb_substr($keyword, 0, 20);
                $reason = $keyword . ' 您的内容包含违规信息，已被系统删除';
                NotifyService::store(0, Notification::TYPE_SYSTEM, $report->id, $report->publish_user_id, $reason, 0, '违规内容被删除');

                $reason = $keyword . ' 您的举报内容已被官方删除，感谢您为干净良好社区环境贡献力量';
                NotifyService::store(0, Notification::TYPE_SYSTEM, $report->id, $report->user_id, $reason, 0, '您举报的内容已被删除');

                $user = User::where('id', $report->publish_user_id)->first();
                if ($user) {
                    $user->punish_times += 1;
                    $user->save();
                    CacheForget::CacheForget($user);
                }
            }

            $report->status = $status;
            $report->save();
        }

        return $this->success([]);
    }

}

